﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Agents.Agent
{
    public partial class NewLocationBasedNotification : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Return to the main page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Agent/AgentMenu.aspx");
        }

        /// <summary>
        /// Occurs when the user adds a notification
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void okBtn_Click(object sender, EventArgs e)
        {
            Queries queries = new Queries();

            // Disable the buttons so the user can't interfere
            backBtn.Enabled = false;
            okBtn.Enabled = false;

            // Get the location from the map
            double x = Double.Parse(hiddenLocationX.Value);
            double y = Double.Parse(hiddenLocationY.Value);

            // Add the notification to the DB
            int notificationId =
                queries.AddLocationBasedNotification(txtContent.Text, x, y,
                txtStreet.Text + " " + txtStreetNum.Text + ", " + txtCity.Text);
                   
            // Announce success and wait for 2 seconds before redirecting
            lblMessage.Text = String.Format("התראה מספר {0} נוספה בהצלחה", 
                                                            notificationId);
            redirectionTimer.Enabled = true;
        }

        /// <summary>
        /// Redirect back to the admin menu after a few seconds
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void redirectionTimer_Tick(object sender, EventArgs e)
        {
            Response.Redirect("~/Agent/AgentMenu.aspx");
        }
    }
}